package com.tz.yiqingapp.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tz.yiqingapp.bean.*;
import com.tz.yiqingapp.config.AccessLimit;
import com.tz.yiqingapp.dto.PageDTO;
import com.tz.yiqingapp.mapper.*;
import com.tz.yiqingapp.utils.MyPasswordEncoder;
import com.tz.yiqingapp.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserHosptial {

    @Autowired
    AdminMapper adminMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserHealthMapper userHealthMapper;
    @Autowired
    HosptialMapper hosptialMapper;
    @Autowired
    UserHealthReportMapper userHealthReportMapper;

    @Autowired
    YimiaoOrderMapper yimiaoOrderMapper;



    @AccessLimit(times = 5)
    @PostMapping("/getHosptialList")
    @ResponseBody
    public HashMap<String, Object> getHosptialList(@RequestBody PageDTO pageDTO, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("user")){
            return ResponseUtils.notlogin("not login");
        }
        List<Hosptial> hosptials = hosptialMapper.selectList(null);

        return ResponseUtils.success(hosptials);
    }

    @AccessLimit(times = 5)
    @PostMapping("/orderHosptial")
    @ResponseBody
    public HashMap<String, Object> orderHosptial(@RequestBody YimiaoOrder yimiaoOrder, HttpServletRequest request) {
        if(request.getSession().getAttribute("loginrole") == null ||
                !request.getSession().getAttribute("loginrole").equals("user")){
            return ResponseUtils.notlogin("not login");
        }
        QueryWrapper<YimiaoOrder> yimiaoOrderQueryWrapper2 = new QueryWrapper<>();
        yimiaoOrderQueryWrapper2.lambda().eq(YimiaoOrder::getUid,(long)request.getSession().getAttribute("loginid"));
        List<YimiaoOrder> yimiaoOrdercheck = yimiaoOrderMapper.selectList(yimiaoOrderQueryWrapper2);
        if(yimiaoOrdercheck.size()>0)
            return ResponseUtils.fail("您预约过医院进行疫苗接种！");

        QueryWrapper<YimiaoOrder> yimiaoOrderQueryWrapper = new QueryWrapper<>();
        yimiaoOrderQueryWrapper.lambda()
                .eq(YimiaoOrder::getHid, yimiaoOrder.getHid())
                .eq(YimiaoOrder::getOrderdate, yimiaoOrder.getOrderdate());
        List<YimiaoOrder> yimiaoOrders = yimiaoOrderMapper.selectList(yimiaoOrderQueryWrapper);
        Hosptial hosptials= hosptialMapper.selectById(yimiaoOrder.getHid());
        if(yimiaoOrders.size()>hosptials.getMaxorder()){
            return ResponseUtils.fail("已达最大预约人数，请预约别的医院");
        }
        yimiaoOrder.setUid((long) request.getSession().getAttribute("loginid"));
        yimiaoOrderMapper.insert(yimiaoOrder);
        return ResponseUtils.success("预约成功！");
    }
}
